package UIComponents
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.text.TextField;
	
	public class SelectWeaponInnerPanel extends Sprite
	{
		private var xhTf:TextField;
		private var wqIcon:IconContainer;
		
		public function SelectWeaponInnerPanel()
		{
			init();
		}
		
		private function init():void
		{
			this.xhTf=this.getChildByName("xhTxt") as TextField;
			this.wqIcon=this.getChildByName("wqIcon") as IconContainer;
			
			addLister();
		}
		
		private function addLister():void
		{
			this.addEventListener(MouseEvent.CLICK,clickFuntion);
		}
		
		private function clickFuntion(e:MouseEvent):void
		{
			if(FlatInfo.newGuiderQHWq && this.item.itemId!=11200002)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTNEWGUIDESD,true));
			}else{
				this.callFun(item);
			}
		}
		private var item:IUseItem;
		private var callFun:Function;
		public function setData(index:Number,iuseItem:IUseItem,callBack:Function):void
		{
			this.reset();
			this.item=iuseItem;
			this.callFun=callBack;
			this.xhTf.text=String(index);
			var tmp:DisplayObject=this.duplicateDisplayObject(item.itemPic,iuseItem,false);
			tmp.scaleX=0.3;
			tmp.scaleY=0.3;
			this.wqIcon.addPic(tmp,this.item.itemName+"："+this.item.description);
		}
		
		/**
		 * 图片克隆 
		 * @param target
		 * @param item
		 * @param autoAdd
		 * @return 
		 */		
		public function duplicateDisplayObject(target:DisplayObject,item:*, autoAdd:Boolean = false):DisplayObject {
			var targetClass:Class = Object(target).constructor;
			var duplicate:DisplayObject = new targetClass(item) as DisplayObject;
			// duplicate properties
			duplicate.transform = target.transform;
			duplicate.filters = target.filters;
			duplicate.cacheAsBitmap = target.cacheAsBitmap;
			duplicate.opaqueBackground = target.opaqueBackground;
			if (target.scale9Grid) {
				var rect:Rectangle = target.scale9Grid;
				if (Capabilities.version.split(" ")[1] == "9,0,16,0"){
					// Flash 9 bug where returned scale9Grid as twips
					rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
				}
				duplicate.scale9Grid = rect;
			}
			// Flash 10 only
			// duplicate.graphics.copyFrom(target.graphics);
			// add to target parent's display list
			// if autoAdd was provided as true
			if (autoAdd && target.parent) {
				target.parent.addChild(duplicate);
			}
			return duplicate;
		}
		
		private function reset():void
		{
			this.item=null;
			this.callFun=null;
			this.xhTf.text="";
			this.wqIcon.addPic(null);
		}
		
		public function getWqicon():Sprite
		{
			return this.wqIcon;
		}
	}
}